package database;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DatabaseHelper extends SQLiteOpenHelper{

	private static DatabaseHelper instance  = null;
	private boolean dbExist = false;
	
	private final static String DB_PATH = "/data/data/com.example.traffic_learning/databases/";
	private final static String DB_NAME = "TrafficRules.db";
	private SQLiteDatabase myDataBase;
	
	private final Context myContext;
	
	//Contructor
	private DatabaseHelper(Context context){
		super(context, DB_NAME, null, 1);
		this.myContext = context;
	}
	
	//Get Instance of object DatabaseHelper
	public static DatabaseHelper getInstance(Context context){
		if(instance == null){
			instance = new DatabaseHelper(context);
		}
		return instance;
	}
	
	@Override
	public void onCreate(SQLiteDatabase db) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
		
	}
	
	@Override
	public synchronized void close() {
		if(getMyDataBase() != null){
			getMyDataBase().close();
		}
		super.close();
	}
	
	public void createDatabase(){
		dbExist = checkDatabase();
		if(!dbExist){
			try{
				this.getReadableDatabase();
			}catch(Exception e){
				Log.e("Traffic Learning", e.toString());
			}
			
			try{
				copyDatabase();
			}catch(IOException e){
				Log.e("Traffic Learning", "Can't copy database: "+e);
			}
		}
	}
	
	public void openDatabase() throws SQLiteException{
		String myPath = DB_PATH + DB_NAME;
		try{
			setMyDataBase(SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE));
		}catch(Exception e){
			
		}
	}
	
	//Method copy database from database is existed
	private void copyDatabase()  throws IOException{
		InputStream myInput = myContext.getAssets().open(DB_NAME);
		String outFileName = DB_PATH + DB_NAME;
		OutputStream myOutput = new FileOutputStream(outFileName);
		byte[] buffer = new byte[512];
		int lenght;
		while ((lenght = myInput.read(buffer)) > 0){
			myOutput.write(buffer, 0, lenght);
		}
		
		myOutput.flush();
		myOutput.close();
		myInput.close();
	}

	//Method check database is exist
	private boolean checkDatabase(){
		SQLiteDatabase checkDB = null;
		try{
			String myPath = DB_PATH + DB_NAME;
			checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
		}catch(SQLiteException e){
			Log.w("Traffic Learning", "Database doesn't exist");
		}
		return checkDB != null ? true : false;
	}

	//Method return object SQLiteDatabase
	public SQLiteDatabase getMyDataBase() {
		return myDataBase;
	}

	private void setMyDataBase(SQLiteDatabase myDataBase) {
		this.myDataBase = myDataBase;
	}

}
